package com.example.graduatetest.view.model;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.graduatetest.activitys.AboutActivity;
import com.example.graduatetest.activitys.AskLeaveActivity;
import com.example.graduatetest.activitys.ChangePasswordActivity;
import com.example.graduatetest.activitys.HomeActivity;
import com.example.graduatetest.activitys.LoginActivity;
import com.example.graduatetest.activitys.NoticeListActivity;
import com.example.graduatetest.bean.Classes;
import com.example.graduatetest.bean.Notice;
import com.example.graduatetest.bean.User;
import com.example.graduatetest.databinding.FragmentMeBinding;
import com.example.graduatetest.model.ClassModel;
import com.example.graduatetest.utils.Constant;

public class MeViewModel extends ViewModel {
    private Activity activity;
    private FragmentMeBinding binding;
    private MutableLiveData<String> name;
    private MutableLiveData<String> status;
    private User user;
    private Intent myIntent;

    public void init(Activity activity, FragmentMeBinding binding){
        this.activity = activity;
        this.binding = binding;
        user = Constant.checkStudentOrTeacher(activity);
        if(name == null) name = new MutableLiveData<>();
        if(status == null) status = new MutableLiveData<>();
        getSomeMessage();
    }

    private void getSomeMessage(){
        SharedPreferences preferences = activity.getSharedPreferences("data", Context.MODE_PRIVATE);
        name.setValue(preferences.getString("name", "默认"));
        if(preferences.getBoolean("isStudent", true)){
            status.setValue("学生");
        }else{
            status.setValue("老师");
        }
    }
    //退出登录
    public void exitLogin(){
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("提示");
        builder.setMessage("是否退出登录？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(activity, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                activity.startActivity(intent);
                activity.finish();
                clearSave(activity.getApplicationContext());
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();  //创建AlertDialog对象
        dialog.show();                           //显示对话框

    }
    //请假处理
    public void askAndDoLeave(){
        if(user.isStudent()){
            myIntent = new Intent(activity, AskLeaveActivity.class);
            myIntent.putExtra("class_number", "NULL");
        }else{
            Classes classes = new Classes();
            myIntent = new Intent(activity, NoticeListActivity.class);
            myIntent.putExtra("type", "请假");
            myIntent.putExtra("classes", classes);
        }
        activity.startActivity(myIntent);
    }

    //跳转到修改密码界面
    public void changePassword(){
        Intent intent = new Intent(activity, ChangePasswordActivity.class);
        activity.startActivity(intent);
    }
    //跳转到关于页面
    public void aboutMessage(){
        Intent intent = new Intent(activity, AboutActivity.class);
        activity.startActivity(intent);
    }

    //清除缓存
    private void clearSave(Context context) {
        SharedPreferences.Editor editor = context.getSharedPreferences("data", Context.MODE_PRIVATE).edit();
        editor.putBoolean("isLogin", false);
        editor.apply();
    }

    public MutableLiveData<String> getName() {
        return name;
    }

    public MutableLiveData<String> getStatus() {
        return status;
    }
}
